package com.nk.system.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.nk.system.service.RegisterService;

/**
 * Servlet implementation class AssessmentServlet2
 */
public class AssessmentServlet2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private RegisterService registerService;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AssessmentServlet2() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		HttpSession userSess = request.getSession();
		
		userSess.setAttribute("gpaxScore2", request.getParameter("gpaxScore"));
		userSess.setAttribute("thaiScore2", request.getParameter("thaiScore"));
		userSess.setAttribute("socialScore2", request.getParameter("socialScore"));
		userSess.setAttribute("engScore2", request.getParameter("engScore"));
		userSess.setAttribute("mathScore2", request.getParameter("mathScore"));
		userSess.setAttribute("scienceScore2", request.getParameter("scienceScore"));
		userSess.setAttribute("sportScore2", request.getParameter("sportScore"));
		userSess.setAttribute("artScore2", request.getParameter("artScore"));
		userSess.setAttribute("techScore2", request.getParameter("techScore"));
		userSess.setAttribute("gatScore2", request.getParameter("gatScore"));
		userSess.setAttribute("pat1Score2", request.getParameter("pat1Score"));
		userSess.setAttribute("pat2Score2", request.getParameter("pat2Score"));
		userSess.setAttribute("pat3Score2", request.getParameter("pat3Score"));
		userSess.setAttribute("pat4Score2", request.getParameter("pat4Score"));
		userSess.setAttribute("pat5Score2", request.getParameter("pat5Score"));
		userSess.setAttribute("pat6Score2", request.getParameter("pat6Score"));
		userSess.setAttribute("pat71Score2", request.getParameter("pat71Score"));
		userSess.setAttribute("pat72Score2", request.getParameter("pat72Score"));
		userSess.setAttribute("pat73Score2", request.getParameter("pat73Score"));
		userSess.setAttribute("pat74Score2", request.getParameter("pat74Score"));
		userSess.setAttribute("pat75Score2", request.getParameter("pat75Score"));
		userSess.setAttribute("pat76Score2", request.getParameter("pat76Score"));
			
		double gpax = Double.parseDouble((String) userSess.getAttribute("gpaxScore2"));
		int thai = Integer.parseInt((String) userSess.getAttribute("thaiScore2"));
		int social = Integer.parseInt((String) userSess.getAttribute("socialScore2"));
		int eng = Integer.parseInt((String) userSess.getAttribute("engScore2"));
		int math = Integer.parseInt((String) userSess.getAttribute("mathScore2"));
		int science = Integer.parseInt((String) userSess.getAttribute("scienceScore2"));
		int sport = Integer.parseInt((String) userSess.getAttribute("sportScore2"));
		int art = Integer.parseInt((String) userSess.getAttribute("artScore2"));
		int tech = Integer.parseInt((String) userSess.getAttribute("techScore2"));
		int gat = Integer.parseInt((String) userSess.getAttribute("gatScore2"));
		int pat1 = Integer.parseInt((String) userSess.getAttribute("pat1Score2"));
		int pat2 = Integer.parseInt((String) userSess.getAttribute("pat2Score2"));
		int pat3 = Integer.parseInt((String) userSess.getAttribute("pat3Score2"));
		int pat5 = Integer.parseInt((String) userSess.getAttribute("pat5Score2"));
		
		if(gpax<=4 && gpax>=0 && thai <= 100 && thai >= 0 && social<=100 && social>=0 && eng <= 100 && eng >= 0 &&
				math<=100 && math>=0 && science <= 100 && science >= 0 && sport<=100 && sport>=0 && art <= 100 && art >= 0 &&
						tech<=100 && tech>=0 && gat <= 300 && gat >= 0 && pat1<=300 && pat1>=0 && pat2 <= 300 && pat2 >= 0 &&
							pat3<=300 && pat3>=0 && pat5 <= 300 && pat5 >= 0){
			if(request.getParameter("save") != null){
				saveScore(request, response);
			}
			
			userSess.setAttribute("gpaxScore", request.getParameter("gpaxScore"));
			userSess.setAttribute("thaiScore", request.getParameter("thaiScore"));
			userSess.setAttribute("socialScore", request.getParameter("socialScore"));
			userSess.setAttribute("engScore", request.getParameter("engScore"));
			userSess.setAttribute("mathScore", request.getParameter("mathScore"));
			userSess.setAttribute("scienceScore", request.getParameter("scienceScore"));
			userSess.setAttribute("sportScore", request.getParameter("sportScore"));
			userSess.setAttribute("artScore", request.getParameter("artScore"));
			userSess.setAttribute("techScore", request.getParameter("techScore"));
			userSess.setAttribute("gatScore", request.getParameter("gatScore"));
			userSess.setAttribute("pat1Score", request.getParameter("pat1Score"));
			userSess.setAttribute("pat2Score", request.getParameter("pat2Score"));
			userSess.setAttribute("pat3Score", request.getParameter("pat3Score"));
			userSess.setAttribute("pat4Score", request.getParameter("pat4Score"));
			userSess.setAttribute("pat5Score", request.getParameter("pat5Score"));
			userSess.setAttribute("pat6Score", request.getParameter("pat6Score"));
			userSess.setAttribute("pat71Score", request.getParameter("pat71Score"));
			userSess.setAttribute("pat72Score", request.getParameter("pat72Score"));
			userSess.setAttribute("pat73Score", request.getParameter("pat73Score"));
			userSess.setAttribute("pat74Score", request.getParameter("pat74Score"));
			userSess.setAttribute("pat75Score", request.getParameter("pat75Score"));
			userSess.setAttribute("pat76Score", request.getParameter("pat76Score"));
			
			request.getRequestDispatcher("AssessmentServlet").forward(request, response);
			return;
		}
		else{
			request.setAttribute("gpaxCheck", "0");
			request.setAttribute("thaiCheck", "0");
			request.setAttribute("socialCheck", "0");
			request.setAttribute("engCheck", "0");
			request.setAttribute("mathCheck", "0");
			request.setAttribute("scienceCheck", "0");
			request.setAttribute("sportCheck", "0");
			request.setAttribute("artCheck", "0");
			request.setAttribute("techCheck", "0");
			request.setAttribute("gatCheck", "0");
			request.setAttribute("pat1Check", "0");
			request.setAttribute("pat2Check", "0");
			request.setAttribute("pat3Check", "0");
			request.setAttribute("pat5Check", "0");
			
			if(gpax>4 || gpax<0){
				request.setAttribute("gpaxCheck", "1");
			}
			if(thai > 100 || thai < 0){
				request.setAttribute("thaiCheck", "1");
			}
			if(social>100 || social<0){
				request.setAttribute("socialCheck", "1");
			}
			if(eng > 100 || eng < 0){
				request.setAttribute("engCheck", "1");
			}
			if(math>100 || math<0){
				request.setAttribute("mathCheck", "1");
			}
			if(science > 100 || science < 0){
				request.setAttribute("scienceCheck", "1");
			}
			if(sport>100 || sport<0){
				request.setAttribute("sportCheck", "1");
			}
			if(art >100 || art < 0){
				request.setAttribute("artCheck", "1");
			}
			if(tech>100 || tech<0){
				request.setAttribute("techCheck", "1");
			}
			if(gat > 300 || gat < 0){
				request.setAttribute("gatCheck", "1");
			}
			if(pat1>300 || pat1<0){
				request.setAttribute("pat1Check", "1");
			}
			if(pat2>300 || pat2<0){
				request.setAttribute("pat2Check", "1");
			}
			if(pat3>300 || pat3<0){
				request.setAttribute("pat3Check", "1");
			}
			if(pat5>300 || pat5<0){
				request.setAttribute("pat5Check", "1");
			}
			
			request.getRequestDispatcher("ScoreServlet").forward(request, response);
			return;
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		HttpSession userSess = request.getSession();
		
		userSess.setAttribute("gpaxScore2", request.getParameter("gpaxScore"));
		userSess.setAttribute("thaiScore2", request.getParameter("thaiScore"));
		userSess.setAttribute("socialScore2", request.getParameter("socialScore"));
		userSess.setAttribute("engScore2", request.getParameter("engScore"));
		userSess.setAttribute("mathScore2", request.getParameter("mathScore"));
		userSess.setAttribute("scienceScore2", request.getParameter("scienceScore"));
		userSess.setAttribute("sportScore2", request.getParameter("sportScore"));
		userSess.setAttribute("artScore2", request.getParameter("artScore"));
		userSess.setAttribute("techScore2", request.getParameter("techScore"));
		userSess.setAttribute("gatScore2", request.getParameter("gatScore"));
		userSess.setAttribute("pat1Score2", request.getParameter("pat1Score"));
		userSess.setAttribute("pat2Score2", request.getParameter("pat2Score"));
		userSess.setAttribute("pat3Score2", request.getParameter("pat3Score"));
		userSess.setAttribute("pat4Score2", request.getParameter("pat4Score"));
		userSess.setAttribute("pat5Score2", request.getParameter("pat5Score"));
		userSess.setAttribute("pat6Score2", request.getParameter("pat6Score"));
		userSess.setAttribute("pat71Score2", request.getParameter("pat71Score"));
		userSess.setAttribute("pat72Score2", request.getParameter("pat72Score"));
		userSess.setAttribute("pat73Score2", request.getParameter("pat73Score"));
		userSess.setAttribute("pat74Score2", request.getParameter("pat74Score"));
		userSess.setAttribute("pat75Score2", request.getParameter("pat75Score"));
		userSess.setAttribute("pat76Score2", request.getParameter("pat76Score"));
			
		double gpax = Double.parseDouble((String) userSess.getAttribute("gpaxScore2"));
		int thai = Integer.parseInt((String) userSess.getAttribute("thaiScore2"));
		int social = Integer.parseInt((String) userSess.getAttribute("socialScore2"));
		int eng = Integer.parseInt((String) userSess.getAttribute("engScore2"));
		int math = Integer.parseInt((String) userSess.getAttribute("mathScore2"));
		int science = Integer.parseInt((String) userSess.getAttribute("scienceScore2"));
		int sport = Integer.parseInt((String) userSess.getAttribute("sportScore2"));
		int art = Integer.parseInt((String) userSess.getAttribute("artScore2"));
		int tech = Integer.parseInt((String) userSess.getAttribute("techScore2"));
		int gat = Integer.parseInt((String) userSess.getAttribute("gatScore2"));
		int pat1 = Integer.parseInt((String) userSess.getAttribute("pat1Score2"));
		int pat2 = Integer.parseInt((String) userSess.getAttribute("pat2Score2"));
		int pat3 = Integer.parseInt((String) userSess.getAttribute("pat3Score2"));
		int pat5 = Integer.parseInt((String) userSess.getAttribute("pat5Score2"));
		
		if(gpax<=4 && gpax>=0 && thai <= 100 && thai >= 0 && social<=100 && social>=0 && eng <= 100 && eng >= 0 &&
				math<=100 && math>=0 && science <= 100 && science >= 0 && sport<=100 && sport>=0 && art <= 100 && art >= 0 &&
						tech<=100 && tech>=0 && gat <= 300 && gat >= 0 && pat1<=300 && pat1>=0 && pat2 <= 300 && pat2 >= 0 &&
							pat3<=300 && pat3>=0 && pat5 <= 300 && pat5 >= 0){
			if(request.getParameter("save") != null){
				saveScore(request, response);
			}
			
			userSess.setAttribute("gpaxScore", request.getParameter("gpaxScore"));
			userSess.setAttribute("thaiScore", request.getParameter("thaiScore"));
			userSess.setAttribute("socialScore", request.getParameter("socialScore"));
			userSess.setAttribute("engScore", request.getParameter("engScore"));
			userSess.setAttribute("mathScore", request.getParameter("mathScore"));
			userSess.setAttribute("scienceScore", request.getParameter("scienceScore"));
			userSess.setAttribute("sportScore", request.getParameter("sportScore"));
			userSess.setAttribute("artScore", request.getParameter("artScore"));
			userSess.setAttribute("techScore", request.getParameter("techScore"));
			userSess.setAttribute("gatScore", request.getParameter("gatScore"));
			userSess.setAttribute("pat1Score", request.getParameter("pat1Score"));
			userSess.setAttribute("pat2Score", request.getParameter("pat2Score"));
			userSess.setAttribute("pat3Score", request.getParameter("pat3Score"));
			userSess.setAttribute("pat4Score", request.getParameter("pat4Score"));
			userSess.setAttribute("pat5Score", request.getParameter("pat5Score"));
			userSess.setAttribute("pat6Score", request.getParameter("pat6Score"));
			userSess.setAttribute("pat71Score", request.getParameter("pat71Score"));
			userSess.setAttribute("pat72Score", request.getParameter("pat72Score"));
			userSess.setAttribute("pat73Score", request.getParameter("pat73Score"));
			userSess.setAttribute("pat74Score", request.getParameter("pat74Score"));
			userSess.setAttribute("pat75Score", request.getParameter("pat75Score"));
			userSess.setAttribute("pat76Score", request.getParameter("pat76Score"));
			
			request.getRequestDispatcher("AssessmentServlet").forward(request, response);
			return;
		}
		else{
			request.setAttribute("gpaxCheck", "0");
			request.setAttribute("thaiCheck", "0");
			request.setAttribute("socialCheck", "0");
			request.setAttribute("engCheck", "0");
			request.setAttribute("mathCheck", "0");
			request.setAttribute("scienceCheck", "0");
			request.setAttribute("sportCheck", "0");
			request.setAttribute("artCheck", "0");
			request.setAttribute("techCheck", "0");
			request.setAttribute("gatCheck", "0");
			request.setAttribute("pat1Check", "0");
			request.setAttribute("pat2Check", "0");
			request.setAttribute("pat3Check", "0");
			request.setAttribute("pat5Check", "0");
			
			if(gpax>4 || gpax<0){
				request.setAttribute("gpaxCheck", "1");
			}
			if(thai > 100 || thai < 0){
				request.setAttribute("thaiCheck", "1");
			}
			if(social>100 || social<0){
				request.setAttribute("socialCheck", "1");
			}
			if(eng > 100 || eng < 0){
				request.setAttribute("engCheck", "1");
			}
			if(math>100 || math<0){
				request.setAttribute("mathCheck", "1");
			}
			if(science > 100 || science < 0){
				request.setAttribute("scienceCheck", "1");
			}
			if(sport>100 || sport<0){
				request.setAttribute("sportCheck", "1");
			}
			if(art >100 || art < 0){
				request.setAttribute("artCheck", "1");
			}
			if(tech>100 || tech<0){
				request.setAttribute("techCheck", "1");
			}
			if(gat > 300 || gat < 0){
				request.setAttribute("gatCheck", "1");
			}
			if(pat1>300 || pat1<0){
				request.setAttribute("pat1Check", "1");
			}
			if(pat2>300 || pat2<0){
				request.setAttribute("pat2Check", "1");
			}
			if(pat3>300 || pat3<0){
				request.setAttribute("pat3Check", "1");
			}
			if(pat5>300 || pat5<0){
				request.setAttribute("pat5Check", "1");
			}
			
			request.getRequestDispatcher("ScoreServlet").forward(request, response);
			return;
		}
	}
	
	private void saveScore(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		
		WebApplicationContext con = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
		registerService = (RegisterService)con.getBean("registerService");
		
		HttpSession userSess = request.getSession();
		int id = (int)userSess.getAttribute("userID");
		
		double gpax = Double.parseDouble(request.getParameter("gpaxScore"));
		int thai = Integer.parseInt(request.getParameter("thaiScore"));
		int social = Integer.parseInt(request.getParameter("socialScore"));
		int eng = Integer.parseInt(request.getParameter("engScore"));
		int math = Integer.parseInt(request.getParameter("mathScore"));
		int science = Integer.parseInt(request.getParameter("scienceScore"));
		int sport = Integer.parseInt(request.getParameter("sportScore"));
		int art = Integer.parseInt(request.getParameter("artScore"));
		int tech = Integer.parseInt(request.getParameter("techScore"));
		int gat = Integer.parseInt(request.getParameter("gatScore"));
		int pat1 = Integer.parseInt(request.getParameter("pat1Score"));
		int pat2 = Integer.parseInt(request.getParameter("pat2Score"));
		int pat3 = Integer.parseInt(request.getParameter("pat3Score"));
		int pat4 = Integer.parseInt(request.getParameter("pat4Score"));
		int pat5 = Integer.parseInt(request.getParameter("pat5Score"));
		int pat6 = Integer.parseInt(request.getParameter("pat6Score"));
		int pat71 = Integer.parseInt(request.getParameter("pat71Score"));
		int pat72 = Integer.parseInt(request.getParameter("pat72Score"));
		int pat73 = Integer.parseInt(request.getParameter("pat73Score"));
		int pat74 = Integer.parseInt(request.getParameter("pat74Score"));
		int pat75 = Integer.parseInt(request.getParameter("pat75Score"));
		int pat76 = Integer.parseInt(request.getParameter("pat76Score"));
		
		registerService.setScore(id, gpax, thai, social, eng, math, science, sport, art, tech, gat, pat1, pat2, pat3, pat4, pat5, pat6, pat71, pat72, pat73, pat74, pat75, pat76);
	}

}
